package cn.zc.curriculum.service.service.impl;


import cn.zc.curriculum.service.config.UploadConfig;
import cn.zc.curriculum.service.service.UploadFileServiceAsync;
import com.aliyun.oss.OSS;
import com.aliyun.oss.model.PutObjectRequest;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.io.*;
import java.util.concurrent.CompletableFuture;

/**
 * 异步处理上传文件方法
 */

@Service
public class UploadFileServiceAsyncImpl implements UploadFileServiceAsync {
    @Resource
    private UploadConfig uploadConfig;

    /**
     * 删除oss上对应路径图片
     * @param ossPath
     * @return
     */
    @Async
    @Override
    public CompletableFuture<Boolean> deleteOssFile(String ossPath) {
        OSS oss=null;

        try {
            oss = uploadConfig.getOss();
            //通过字符串截取，取出文件对应位置
            String imgPath = ossPath.substring(ossPath.lastIndexOf(uploadConfig.getAliOssEndpoint()) + uploadConfig.getAliOssEndpoint().length() + 1);
            oss.deleteObject(uploadConfig.getAliOssbucketName(),imgPath);
        }catch (Exception e){
            e.printStackTrace();
            return CompletableFuture.completedFuture(false);
        }finally {
            if(oss != null){
                oss.shutdown();
            }
        }
        return CompletableFuture.completedFuture(true);
    }


    /**
     * 删除本地文件
     */
    @Async
    @Override
    public void deleteLocalFile(String filePath){
        File file = new File(filePath);
        // 路径为文件且不为空则进行删除
        if (file.isFile() && file.exists()) {
            file.delete();
        }
    }


    /**
     * 保存至本地
     * @param fileStream
     * @param filePath
     * @param
     * @return
     * @throws IOException
     */
    @Async
    public void saveLocal(InputStream fileStream, String filePath, String localPath) throws IOException {


        byte[] bytes = new byte[1024];
        FileOutputStream outputStream =null;
        try {
            File dest = new File(uploadConfig.getLocalSavePath() + filePath);
            if (!dest.isDirectory() && !dest.exists()) {
                dest.mkdirs();
            }
            File path = new File(localPath);
            outputStream = new FileOutputStream(path);

            outputStream.write(fileStream.readAllBytes());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            if(outputStream != null){
                outputStream.close();
                fileStream.close();
            }
        }
    }




    /**
     * 阿里云图片oss上传点
     * @param fileStream
     * @param fileSavePath
     */
    @Async
    @Override
    public void uploadToOss(InputStream fileStream, String fileSavePath){
        //判断图片是否要保存至阿里云Oss
        if(uploadConfig.isImgOssSave()) {
            OSS oss = null;
            try {
                oss = uploadConfig.getOss();
                //发送至阿里云oss
                PutObjectRequest putObjectRequest = new PutObjectRequest(uploadConfig.getAliOssbucketName(), fileSavePath
                        , new ByteArrayInputStream(fileStream.readAllBytes()), uploadConfig.getImgMetaData());

                oss.putObject(putObjectRequest);

            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (oss != null) {
                    oss.shutdown();
                    try {
                        fileStream.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}
